package library.view;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import library.controller.LibraryControllerListener;
import library.dao.Impl.UserDaoImpl;
import library.pojo.User;

@SuppressWarnings("serial")
public class UpdateMessage extends JPanel {
	private JTextField oldTextField;
	private JTextField newTextField;
	private JTextField isNewTextField;
	private JTextField nameTextField;
	private User user;
	private TotalFrameSystem t1;
	private TotalFrameCommon t2;
	private TotalFrameBook t3;
	private ImageIcon icon;
	private Image img;

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(img,0,0,this.getWidth(),this.getHeight(),this);
	}

	public UpdateMessage(LibraryControllerListener libraryControllerListener, User user, TotalFrameSystem t1,TotalFrameCommon t2,TotalFrameBook t3) {
		setLayout(null);
		this.user = user;
		this.t1 = t1;
		this.t2 = t2;
		this.t3 = t3;		

		icon = new ImageIcon("res/2.png");
		img = icon.getImage();
		
		JLabel lblNewLabel = new JLabel("用户名：");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		lblNewLabel.setBounds(403, 60, 102, 44);
		add(lblNewLabel);
		
		JLabel oneLabel = new JLabel();
		oneLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		oneLabel.setIcon(new ImageIcon("res/user1.png"));
		oneLabel.setBounds(353, 105, 40, 40);
		add(oneLabel);
		
		oldTextField = new JTextField();
		oldTextField.setFont(new Font("宋体", Font.PLAIN, 20));
		oldTextField.setBounds(403, 105, 384, 44);
		oldTextField.setText(user.getUsername());
		oldTextField.setEditable(false);
		add(oldTextField);

		JLabel lblNewLabel_1 = new JLabel("姓名：");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 25));
		lblNewLabel_1.setBounds(403, 180, 102, 44);
		add(lblNewLabel_1);

		JLabel twoLabel = new JLabel();
		twoLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		twoLabel.setIcon(new ImageIcon("res/name.png"));
		twoLabel.setBounds(353, 225, 40, 40);
		add(twoLabel);
		
		newTextField = new JTextField();
		newTextField.setFont(new Font("宋体", Font.PLAIN, 20));
		newTextField.setEditable(false);
		newTextField.setBounds(403, 225, 384, 44);
		newTextField.setText(user.getName());
		add(newTextField);

		JLabel lblNewLabel_1_1 = new JLabel("联系电话：");
		lblNewLabel_1_1.setFont(new Font("宋体", Font.PLAIN, 25));
		lblNewLabel_1_1.setBounds(403, 300, 130, 44);
		add(lblNewLabel_1_1);

		JLabel threeLabel = new JLabel();
		threeLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		threeLabel.setIcon(new ImageIcon("res/phone.png"));
		threeLabel.setBounds(353, 345, 40, 40);
		add(threeLabel);
		
		isNewTextField = new JTextField();
		isNewTextField.setFont(new Font("宋体", Font.PLAIN, 20));
		isNewTextField.setEditable(false);
		isNewTextField.setBounds(403, 345, 384, 44);
		isNewTextField.setText(user.getPhone());
		add(isNewTextField);

		JLabel fourLabel = new JLabel();
		fourLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		fourLabel.setIcon(new ImageIcon("res/type.png"));
		fourLabel.setBounds(353, 465, 40, 40);
		add(fourLabel);
		
		JLabel lblNewLabel_1_1_1 = new JLabel("用户类型：");
		lblNewLabel_1_1_1.setFont(new Font("宋体", Font.PLAIN, 25));
		lblNewLabel_1_1_1.setBounds(403, 420, 142, 44);
		add(lblNewLabel_1_1_1);

		nameTextField = new JTextField();
		nameTextField.setFont(new Font("宋体", Font.PLAIN, 20));
		nameTextField.setEditable(false);
		nameTextField.setBounds(403, 465, 384, 44);
		nameTextField.setText(user.getType());
		add(nameTextField);

		JButton updateButton = new JButton("修改信息");
		updateButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateActionPerformed(e);
			}
		});
		updateButton.setFont(new Font("宋体", Font.PLAIN, 20));
		updateButton.setBounds(403, 565, 384, 44);
		add(updateButton);
	}

	protected void updateActionPerformed(ActionEvent e) {
		UpdateMessageDialog update = new UpdateMessageDialog("修改信息", user, t1,t2,t3);
	}

	/**
	 * 判断是否为空
	 */
	private boolean isEmpty(String str) {
		if (str == null || "".equals(str.trim())) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断两次输入的密码是否一致
	 */
	private boolean isNotSame(String str1, String str2) {
		if (str1.equals(str2)) {
			return false;
		} else {
			return true;
		}
	}
}
